package com.xray.netdisk.utils;

import com.xray.netdisk.pojo.FileItem;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.List;
import static com.xray.netdisk.utils.FileUtils.*;

/**
 * 网络工具类
 */
public class NetworkUtils {

    /**
     * 从服务器获得文件列表
     * @return
     */
    public static List<FileItem> getFileList(){
        try(Socket socket = new Socket(IP,PORT);
            DataInputStream input = new DataInputStream(socket.getInputStream());
            DataOutputStream output = new DataOutputStream(socket.getOutputStream())){
            //发送命令类型
            output.writeInt(TYPE_LIST);
            //从服务器端获得JSON
            String json = input.readUTF();
            //解析JSON返回集合
            List<FileItem> fileItems = jsonToList(json);
            return fileItems;
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 上传文件
     * @param path
     */
    public static void uploadFile(String path){
        try(Socket socket = new Socket(IP,PORT);
            DataOutputStream output = new DataOutputStream(socket.getOutputStream())){
            //发送命令类型
            output.writeInt(TYPE_UPLOAD);
            File file = new File(path);
            if(file.exists()){
                //如果文件存在，发送文件名给服务器
                output.writeUTF(file.getName());
                //文件传输给服务器
                FileInputStream fileInputStream = new FileInputStream(file);
                FileUtils.io(fileInputStream,output);
                System.out.println("文件上传完毕");
            }else{
                //如果文件不存在，发送错误信息
                output.writeUTF("error");
                System.out.println("对不起，文件不存在！");
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 下载文件
     * @param name
     */
    public static void downloadFile(String name){
        try(Socket socket = new Socket(IP,PORT);
            DataInputStream input = new DataInputStream(socket.getInputStream());
            DataOutputStream output = new DataOutputStream(socket.getOutputStream())){
            //发送命令类型
            output.writeInt(TYPE_DOWNLOAD);
            //发送文件名
            output.writeUTF(name);
            //读取文件名
            String result = input.readUTF();
            //判断文件是否存在
            if("error".equals(result)){
                System.out.println("服务器没有此文件");
                return;
            }
            //如果下载目录不存在，就创建目录
            File downloadDir = new File(DOWNLOAD_DIR);
            if(!downloadDir.exists()){
                downloadDir.mkdir();
            }
            //下载文件
            FileOutputStream fileOutputStream = new FileOutputStream(DOWNLOAD_DIR + "\\" + name);
            FileUtils.io(input,fileOutputStream);
            System.out.println("文件下载完毕");
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除文件
     * @param name
     */
    public static void deleteFile(String name){
        try(Socket socket = new Socket(IP,PORT);
            DataInputStream input = new DataInputStream(socket.getInputStream());
            DataOutputStream output = new DataOutputStream(socket.getOutputStream())){
            //发送命令类型
            output.writeInt(TYPE_DELETE);
            //发送文件名
            output.writeUTF(name);
            //获得删除结果
            String result = input.readUTF();
            if("error".equals(result)){
                System.out.println("服务器没有此文件");
                return;
            }
            System.out.println("文件删除完毕");
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
